package com.example.myapplication

import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.ShoppingCart
import androidx.compose.material.icons.outlined.Home
import androidx.compose.material.icons.outlined.Menu
import androidx.compose.material.icons.outlined.Person
import androidx.compose.material.icons.outlined.ShoppingCart
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.myapplication.ui.common.BottomAppBarItem
import com.example.myapplication.ui.screen.CategoryScreen
import com.example.myapplication.ui.screen.HomeScreen
import com.example.myapplication.ui.screen.LoginScreen
import com.example.myapplication.ui.screen.ProductDetailScreen
import com.example.myapplication.ui.screen.ShoppingCartScreen

@Composable
fun MainScreen() {
    //当前页索引
    var currentPageIndex = rememberSaveable { mutableIntStateOf(0) }

    //导航控制器
    val navController = rememberNavController()
    //获取当前栈顶的页面信息
    val navBackStackEntry = navController.currentBackStackEntryAsState()
    //获取栈顶的子页的路由路径
    val currentRoute = navBackStackEntry?.value?.destination?.route
    println("当前路由是：" + currentRoute)

    // 是否需要显示 Scaffold（底部栏）
    val bottomItems = listOf("首页", "分类", "购物车","我的")
    val mainRouters = listOf("home", "category", "shoppingCart","profile")

    //将需要显示底部导航栏的路径保存起来
    val showBottomBar = currentRoute in listOf("home", "category", "shoppingCart")

    //用来搭建页面的结构（底部导航栏+顶部标题栏）
    Scaffold(
        bottomBar = {
            if (showBottomBar){
                BottomAppBar(
                    actions = {
                        bottomItems.forEachIndexed { index, item ->
                            BottomAppBarItem(
                                normalIcon = when (index) {
                                    0 -> Icons.Outlined.Home
                                    1 -> Icons.Outlined.Menu
                                    2 -> Icons.Outlined.ShoppingCart
                                    else -> Icons.Outlined.Person
                                },
                                selectedIcon = when (index) {
                                    0 -> Icons.Filled.Home
                                    1 -> Icons.Filled.Menu
                                    2 -> Icons.Filled.ShoppingCart
                                    else -> Icons.Filled.Person
                                },
                                text = item,
                                selected = currentPageIndex.value == index,
                                onClick = {
                                    currentPageIndex.value = index
                                    if (index!=3){
                                        navController.navigate(mainRouters[index]) {
                                            popUpTo(navController.graph.startDestinationId) {
                                                saveState = true
                                            }
                                            launchSingleTop = true
                                        }
                                    }else{
                                        navController.navigate(mainRouters[index])
                                    }
                                },
                                modifier = Modifier.weight(1F)
                            )
                        }
                    }
                )
            }
        }
    ) { innerPadding ->
        NavHost(
            navController = navController,
            startDestination = "home",
            modifier = Modifier.padding(innerPadding)
        ) {
            composable("home") { HomeScreen(navController) }
            composable("category") { CategoryScreen() }
            composable("shoppingCart") { ShoppingCartScreen() }
            composable("profile") { LoginScreen(navController) }
            composable("productDetail") { ProductDetailScreen(navController) }
        }
    }
}